package com.itheima_007;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class PictureFrame extends JFrame {
    private int[][] datas = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 0}
    };

    //定义成功移动后的数组
    private int[][] winDatas = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 0}
    };
    //定义两个int型变量 记录0号图片位置
    private int x0;
    private int y0;
    //定义上左下右重置帮助按钮
    private JButton shangButton;
    private JButton zuoButton;
    private JButton xiaButton;
    private JButton youButton;
    private JButton qiuzhuButton;
    private JButton chongzhiButton;
    private JPanel imagePanel;

    public PictureFrame() {
        //初始化窗体
        initFrame();

        //打乱图片（二维数组）
        randomData();

        //绘制窗体
        paintView();

        //给按钮增加事件
        addButtonEvent();

        this.setVisible(true);
    }

    //初始化窗体
    public void initFrame() {
        //窗体大小
        this.setSize(960, 565);
        //窗体标题
        this.setTitle("动漫拼图");
        //窗体居中
        this.setLocationRelativeTo(null);
        //窗体关闭时退出应用程序
        this.setDefaultCloseOperation(3);
        //窗体位于其他窗体之上
        this.setAlwaysOnTop(true);
        //取消窗体默认布局
        this.setLayout(null);
    }

    //窗体上组件的绘制
    public void paintView() {
        //标题图片
        JLabel titleLabel = new JLabel(new ImageIcon("C:\\Users\\Administrator.DESKTOP-88TF7H0\\IdeaProjects\\picture_puzzle\\images\\title.png"));
        titleLabel.setBounds(354, 27, 232, 57);
        this.add(titleLabel);

        //定义一个二维数组，用来存储图片的编号
//        int[][] datas = {
//                {1, 2, 3, 4},
//                {5, 6, 7, 8},
//                {9, 10, 11, 12},
//                {13, 14, 15, 16}
//        };
        //创建面板
        imagePanel = new JPanel();
        imagePanel.setBounds(150, 114, 360, 360);
        imagePanel.setLayout(null);

        //遍历二维数组，得到图片编号
        for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j < datas[i].length; j++) {
                //创建Jlabel类创建对象，加载图片资源
                JLabel imageLabel = new JLabel(new ImageIcon("C:\\Users\\Administrator.DESKTOP-88TF7H0\\IdeaProjects\\picture_puzzle\\images\\" + datas[i][j] + ".png"));
                imageLabel.setBounds(j * 90, i * 90, 90, 90);
                imagePanel.add(imageLabel);
            }
        }
        this.add(imagePanel);

        //参照图
        JLabel canzhaoLabel = new JLabel(new ImageIcon("C:\\Users\\Administrator.DESKTOP-88TF7H0\\IdeaProjects\\picture_puzzle\\images\\canzhaotu.png"));
        canzhaoLabel.setBounds(574, 114, 122, 121);
        this.add(canzhaoLabel);

        //上下左右按钮 求助按钮 重置按钮
        shangButton = new JButton(new ImageIcon("C:\\Users\\Administrator.DESKTOP-88TF7H0\\IdeaProjects\\picture_puzzle\\images\\shang.png"));
        shangButton.setBounds(732, 265, 57, 57);
        this.add(shangButton);

        xiaButton = new JButton(new ImageIcon("C:\\Users\\Administrator.DESKTOP-88TF7H0\\IdeaProjects\\picture_puzzle\\images\\xia.png"));
        xiaButton.setBounds(732, 347, 57, 57);
        this.add(xiaButton);

        zuoButton = new JButton(new ImageIcon("C:\\Users\\Administrator.DESKTOP-88TF7H0\\IdeaProjects\\picture_puzzle\\images\\zuo.png"));
        zuoButton.setBounds(650, 347, 57, 57);
        this.add(zuoButton);

        youButton = new JButton(new ImageIcon("C:\\Users\\Administrator.DESKTOP-88TF7H0\\IdeaProjects\\picture_puzzle\\images\\you.png"));
        youButton.setBounds(813, 347, 57, 57);
        this.add(youButton);

        qiuzhuButton = new JButton(new ImageIcon("C:\\Users\\Administrator.DESKTOP-88TF7H0\\IdeaProjects\\picture_puzzle\\images\\qiuzhu.png"));
        qiuzhuButton.setBounds(626, 444, 108, 45);
        this.add(qiuzhuButton);

        chongzhiButton = new JButton(new ImageIcon("C:\\Users\\Administrator.DESKTOP-88TF7H0\\IdeaProjects\\picture_puzzle\\images\\chongzhi.png"));
        chongzhiButton.setBounds(786, 444, 108, 45);
        this.add(chongzhiButton);

        //设置背景图
        JLabel backgroundLabel = new JLabel(new ImageIcon("C:\\Users\\Administrator.DESKTOP-88TF7H0\\IdeaProjects\\picture_puzzle\\images\\background.png"));
        backgroundLabel.setBounds(0, 0, 960, 530);
        this.add(backgroundLabel);


    }

    //二维数组打乱
    public void randomData() {
        Random r = new Random();
        for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j < datas[i].length; j++) {
                int x = r.nextInt(datas.length);
                int y = r.nextInt(datas[i].length);

                int temp = datas[i][j];
                datas[i][j] = datas[x][y];
                datas[x][y] = temp;
            }

        }
        //记录0号图片的位置
        wc:
        for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j < datas[i].length; j++) {
                if (datas[i][j] == 0) {
                    x0 = i;
                    y0 = j;
                    break wc;
                }
            }

        }

    }

    //移动成功的操作
    public void success() {
        datas = new int[][]{
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12},
                {13, 14, 15, 16}
        };
        shangButton.setEnabled(false);
        zuoButton.setEnabled(false);
        xiaButton.setEnabled(false);
        youButton.setEnabled(false);
    }

    //移动的图形重绘
    public void rePaintView() {
        imagePanel.removeAll();
        for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j < datas[i].length; j++) {
                //创建Jlabel类创建对象，加载图片资源
                JLabel imageLabel = new JLabel(new ImageIcon("C:\\Users\\Administrator.DESKTOP-88TF7H0\\IdeaProjects\\picture_puzzle\\images\\" + datas[i][j] + ".png"));
                imageLabel.setBounds(j * 90, i * 90, 90, 90);
                imagePanel.add(imageLabel);
            }
        }
        this.add(imagePanel);
        //重新绘制窗体
        imagePanel.repaint();
    }

    //给按钮添加事件
    public void addButtonEvent() {
        shangButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                System.out.println("上");
                if (x0 == 3) {
                    return;
                }
                //位置交换
                datas[x0][y0] = datas[x0 + 1][y0];
                datas[x0 + 1][y0] = 0;
                x0 = x0 + 1;
                //判断是否已经拼完
                if (isSuccess()) {
                    success();
                }
                //调用重绘的方法
                rePaintView();

            }
        });
        zuoButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (y0 == 3) {
                    return;
                }
                datas[x0][y0] = datas[x0][y0 + 1];
                datas[x0][y0 + 1] = 0;
                y0 = y0 + 1;
                if (isSuccess()) {
                    success();
                }
                rePaintView();
            }
        });
        xiaButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (x0 == 0) {
                    return;
                }
                datas[x0][y0] = datas[x0 - 1][y0];
                datas[x0 - 1][y0] = 0;
                x0 = x0 - 1;
                if (isSuccess()) {
                    success();
                }
                rePaintView();
            }
        });
        youButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (y0 == 0) {
                    return;
                }
                datas[x0][y0] = datas[x0][y0 - 1];
                datas[x0][y0 - 1] = 0;
                y0 = y0 - 1;
                if (isSuccess()) {
                    success();
                }
                rePaintView();
            }
        });
        chongzhiButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                System.out.println("重置");
                    datas=new int[][]{
                            {1,2,3,4},
                            {5,6,7,8},
                            {9,10,11,12},
                            {13,14,15,0}
                    };
                    randomData();
                    rePaintView();
                    shangButton.setEnabled(true);
                    zuoButton.setEnabled(true);
                    xiaButton.setEnabled(true);
                    youButton.setEnabled(true);
            }
        });
        qiuzhuButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                System.out.println("求助");
                success();
                rePaintView();
            }
        });

    }

    //判断移动是否成功
    public boolean isSuccess(){
        for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j < datas[i].length; j++) {
                if (datas[i][j]!=winDatas[i][j])return false;
            }
        }
        return true;
    }


}
